home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Archivers / PPCUnARJ / source / unarj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-18  |  22.9 KB  |  1,023 lines

  1. /* UNARJ.C, UNARJ, R JUNG, 02/17/93 (Amiga Port 03/18/95)
  2.  * Main Extractor routine
  3.  * Copyright (c) 1991-93 by Robert K Jung.  All rights reserved.
  4.  * Amiga Port done by Andreas R. Kleinert in 1995-97, powerUP (TM) in 1998
  5.  *
  6.  *   This code may be freely used in programs that are NOT ARJ archivers
  7.  *   (both compress and extract ARJ archives).
  8.  *
  9.  *   If you wish to distribute a modified version of this program, you
  10.  *   MUST indicate that it is a modified version both in the program and
  11.  *   source code.
  12.  *
  13.  *   I am holding the copyright on the source code, so please do not
  14.  *   delete my name from the program files or from the documentation.
  15.  *
  16.  *   I wish to give credit to Haruhiko Okumura for providing the
  17.  *   basic ideas for ARJ and UNARJ in his program AR.  Please note
  18.  *   that UNARJ is significantly different from AR from an archive
  19.  *   structural point of view.
  20.  *
  21.  * Modification history:
  22.  * Date      Programmer  Description of modification.
  23.  * 04/05/91  R. Jung     Rewrote code.
  24.  * 04/23/91  M. Adler    Portabilized.
  25.  * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
  26.  *                       fillbuf().
  27.  * 05/19/91  R. Jung     Fixed extended header skipping code.
  28.  * 05/25/91  R. Jung     Improved find_header().
  29.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  30.  *                       set_ftime_mode().
  31.  * 06/19/81  R. Jung     Added two more %c in printf() in list_arc().
  32.  * 07/07/91  R. Jung     Added default_case_path() to extract().
  33.  *                       Added strlower().
  34.  * 07/20/91  R. Jung     Changed uint ratio() to static uint ratio().
  35.  * 07/21/91  R. Jung     Added #ifdef VMS.
  36.  * 08/28/91  R. Jung     Changed M_DIFFHOST message.
  37.  * 08/31/91  R. Jung     Added changes to support MAC THINK_C compiler
  38.  *                       per Eric Larson.
  39.  * 10/07/91  R. Jung     Added missing ; to THINK_C additions.
  40.  * 11/11/91  R. Jung     Added host_os test to fwrite_txt_crc().
  41.  * 11/24/91  R. Jung     Added more error_count processing.
  42.  * 12/03/91  R. Jung     Added backup file processing.
  43.  * 02/17/93  R. Jung     Added archive modified date support.
  44.  * 03/18/95  A. Kleinert Ported V2.41 to Amiga. Did some necessary changes.
  45.  * 10/27/97  A. Kleinert recompiled with SAS/C 6.58
  46.  * 02/07/98  A. Kleinert ported to powerUP (TM), using SAS/C for PPC
  47.  * 02/17/98  A. Kleinert updated
  48.  *
  49.  */
  50.  
  51. #include "unarj.h"
  52.  
  53. #ifdef MODERN
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <ctype.h>
  57. #else /* !MODERN */
  58. extern void free();
  59. extern void exit();
  60. extern char *strcat();
  61. extern char *strcpy();
  62. extern char *strncpy();
  63. extern char *strchr();
  64. extern char *strrchr();
  65. extern int strlen();
  66. extern int strcmp();
  67. #ifdef VMS
  68. #include <ssdef.h>
  69. #define EXIT_FAILURE SS$_ABORT
  70. #define EXIT_SUCCESS SS$_NORMAL
  71. #else
  72. #define EXIT_FAILURE (1)
  73. #define EXIT_SUCCESS (0)
  74. #endif
  75. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  76. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  77. #endif /* ?MODERN */
  78.  
  79. #ifdef THINK_C
  80. #include <console.h>
  81. #endif
  82.  
  83. /* Global variables */
  84.  
  85. UCRC   crc;
  86. FILE   *arcfile;
  87. FILE   *outfile;
  88. ushort bitbuf;
  89. long   compsize;
  90. long   origsize;
  91. uchar  subbitbuf;
  92. uchar  header[HEADERSIZE_MAX];
  93. char   arc_name[FNAME_MAX];
  94. int    command;
  95. int    bitcount;
  96. int    file_type;
  97. int    no_output;
  98. int    error_count;
  99.  
  100. /* Messages */
  101.  
  102. static char *M_USAGE  [] =
  103. {
  104. "Usage:  UNARJ archive[.arj]    (list archive)\n",
  105. "        UNARJ e archive        (extract archive)\n",
  106. "        UNARJ l archive        (list archive)\n",
  107. "        UNARJ t archive        (test archive)\n",
  108. "        UNARJ x archive        (extract with pathnames)\n",
  109. "\n",
  110. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  111. "You may freely use, copy and distribute this program, provided that no fee\n",
  112. "is charged for such use, copying or distribution, and it is distributed\n",
  113. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  114. "warranty of any kind, express or implied, including but not limited to\n",
  115. "the implied warranties of merchantability and fitness for a particular\n",
  116. "purpose.  Refer to UNARJ.DOC for more warranty information.\n",
  117. "\n",
  118. "ARJ Software                    Internet address :  robjung@world.std.com\n",
  119. "Robert and Susan Jung           CompuServe userid:  72077,445\n",
  120. "2606 Village Road West\n",
  121. "Norwood, Massachusetts 02062\n",
  122. "USA\n",
  123. "\npowerUP (TM) port has been done by Andreas R. Kleinert",
  124. "\nEMail: Andreas_Kleinert@t-online.de\n\n",
  125. NULL
  126. };
  127.  
  128. char M_VERSION [] = "UNARJ (Demo version) 2.41 Copyright (c) 1991-93 Robert K Jung\n"
  129.                     "powerUP (TM) Port V1.03 of UnARJ 2.41, done by Andreas R. Kleinert in 1998\n\n";
  130.  
  131. char M_ARCDATE [] = "Archive created: %s";
  132. char M_ARCDATEM[] = ", modified: %s";
  133. char M_BADCOMND[] = "Bad UNARJ command: %s";
  134. char M_BADCOMNT[] = "Invalid comment header";
  135. char M_BADHEADR[] = "Bad header";
  136. char M_BADTABLE[] = "Bad Huffman code";
  137. char M_CANTOPEN[] = "Can't open %s";
  138. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  139. char M_CANTWRIT[] = "Can't write file. Disk full?";
  140. char M_CRCERROR[] = "CRC error!\n";
  141. char M_CRCOK   [] = "CRC OK\n";
  142. char M_DIFFHOST[] = "  Binary file!";
  143. char M_ENCRYPT [] = "File is password encrypted, ";
  144. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  145. char M_EXTRACT [] = "Extracting %-25s";
  146. char M_FEXISTS [] = "%-25s exists, ";
  147. char M_HEADRCRC[] = "Header CRC error!";
  148. char M_NBRFILES[] = "%5d file(s)\n";
  149. char M_NOMEMORY[] = "Out of memory";
  150. char M_NOTARJ  [] = "%s is not an ARJ archive";
  151. char M_PROCARC [] = "Processing archive: %s\n";
  152. char M_SKIPPED [] = "Skipped %s\n";
  153. char M_SUFFIX  [] = ARJ_SUFFIX;
  154. char M_TESTING [] = "Testing    %-25s";
  155. char M_UNKNMETH[] = "Unsupported method: %d, ";
  156. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  157. char M_UNKNVERS[] = "Unsupported version: %d, ";
  158.  
  159. #define get_crc()       get_longword()
  160. #define fget_crc(f)     fget_longword(f)
  161.  
  162. #define setup_get(PTR)  (get_ptr = (PTR))
  163. #define get_byte()      ((uchar)(*get_ptr++ & 0xff))
  164.  
  165. #define BUFFERSIZE      4096
  166.  
  167. #define ASCII_MASK      0x7F
  168.  
  169. #define CRCPOLY         0xEDB88320L
  170.  
  171. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  172.  
  173. /* Local functions */
  174.  
  175. #ifdef MODERN
  176. static void  make_crctable(void);
  177. static void  crc_buf(char *str, int len);
  178. static void  strparity(uchar *p);
  179. static FILE  *fopen_msg(char *name, char *mode);
  180. static int   fget_byte(FILE *f);
  181. static uint  fget_word(FILE *f);
  182. static ulong fget_longword(FILE *f);
  183. static void  fread_crc(uchar *p, int n, FILE *f);
  184. static void  decode_path(char *name);
  185. static void  get_date_str(char *str, ulong tstamp);
  186. static int   parse_path(char *pathname, char *path, char *entry);
  187. static void  strncopy(char *to, char *from, int len);
  188. static uint  get_word(void);
  189. static ulong get_longword(void);
  190. static long  find_header(FILE *fd);
  191. static int   read_header(int first, FILE *fd, char *name);
  192. static void  skip(void);
  193. static void  unstore(void);
  194. static int   check_flags(void);
  195. static int   extract(void);
  196. static int   test(void);
  197. static uint  ratio(long a, long b);
  198. static void  list_start(void);
  199. static void  list_arc(int count);
  200. static void  execute_cmd(void);
  201. static void  help(void);
  202. #endif /* MODERN */
  203.  
  204. /* Local variables */
  205.  
  206. static char   filename[FNAME_MAX];
  207. static char   comment[COMMENT_MAX];
  208. static char   *hdr_filename;
  209. static char   *hdr_comment;
  210.  
  211. static ushort headersize;
  212. static uchar  first_hdr_size;
  213. static uchar  arj_nbr;
  214. static uchar  arj_x_nbr;
  215. static uchar  host_os;
  216. static uchar  arj_flags;
  217. static short  method;
  218. static uint   file_mode;
  219. static ulong  time_stamp;
  220. static short  entry_pos;
  221. static ushort host_data;
  222. static uchar  *get_ptr;
  223. static UCRC   file_crc;
  224. static UCRC   header_crc;
  225.  
  226. static long   first_hdr_pos;
  227. static long   torigsize;
  228. static long   tcompsize;
  229.  
  230. static int    clock_inx;
  231.  
  232. static char   *writemode[2]  = { "wb",  "w" };
  233.  
  234. static UCRC   crctable[UCHAR_MAX + 1];
  235.  
  236. /* Functions */
  237.  
  238. static void
  239. make_crctable()
  240. {
  241.     uint i, j;
  242.     UCRC r;
  243.  
  244.     for (i = 0; i <= UCHAR_MAX; i++)
  245.     {
  246.         r = i;
  247.         for (j = CHAR_BIT; j > 0; j--)
  248.         {
  249.             if (r & 1)
  250.                 r = (r >> 1) ^ CRCPOLY;
  251.             else
  252.                 r >>= 1;
  253.         }
  254.         crctable[i] = r;
  255.     }
  256. }
  257.  
  258. static void
  259. crc_buf(str, len)
  260. char *str;
  261. int  len;
  262. {
  263.     while (len--)
  264.         UPDATE_CRC(crc, *str++);
  265. }
  266.  
  267. void
  268. disp_clock()
  269. {
  270.     static char clock_str[4] = { '|', '/', '-', '\\' };
  271.  
  272.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  273.     clock_inx = (clock_inx + 1) & 0x03;
  274. }
  275.  
  276. void
  277. error(fmt, arg)
  278. char *fmt;
  279. char *arg;
  280. {
  281.     putc('\n', stdout);
  282.     printf(fmt, arg, error_count);
  283.     putc('\n', stdout);
  284.     exit(EXIT_FAILURE);
  285. }
  286.  
  287. static void
  288. strparity(p)
  289. uchar *p;
  290. {
  291.     while (*p)
  292.     {
  293.         FIX_PARITY(*p);
  294.         p++;
  295.     }
  296. }
  297.  
  298. static FILE *
  299. fopen_msg(name, mode)
  300. char *name;
  301. char *mode;
  302. {
  303.     FILE *fd;
  304.  
  305.     fd = file_open(name, mode);
  306.     if (fd == NULL)
  307.         error(M_CANTOPEN, name);
  308.     return fd;
  309. }
  310.  
  311. static int
  312. fget_byte(f)
  313. FILE *f;
  314. {
  315.     int c;
  316.  
  317.     if ((c = getc(f)) == EOF)
  318.         error(M_CANTREAD, "");
  319.     return c & 0xFF;
  320. }
  321.  
  322. static uint
  323. fget_word(f)
  324. FILE *f;
  325. {
  326.     uint b0, b1;
  327.  
  328.     b0 = fget_byte(f);
  329.     b1 = fget_byte(f);
  330.     return (b1 << 8) + b0;
  331. }
  332.  
  333. static ulong
  334. fget_longword(f)
  335. FILE *f;
  336. {
  337.     ulong b0, b1, b2, b3;
  338.  
  339.     b0 = fget_byte(f);
  340.     b1 = fget_byte(f);
  341.     b2 = fget_byte(f);
  342.     b3 = fget_byte(f);
  343.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  344. }
  345.  
  346. static void
  347. fread_crc(p, n, f)
  348. uchar *p;
  349. int   n;
  350. FILE  *f;
  351. {
  352.     n = file_read((char *)p, 1, n, f);
  353.     origsize += n;
  354.     crc_buf((char *)p, n);
  355. }
  356.  
  357. void
  358. fwrite_txt_crc(p, n)
  359. uchar *p;
  360. int   n;
  361. {
  362.     uchar c;
  363.  
  364.     crc_buf((char *)p, n);
  365.     if (no_output)
  366.         return;
  367.  
  368.     if (file_type == TEXT_TYPE)
  369.     {
  370.         while (n--)
  371.         {
  372.             c = *p++;
  373.             if (host_os != OS)
  374.             {
  375.                 FIX_PARITY(c);
  376.             }
  377.             if (putc((int) c, outfile) == EOF)
  378.                 error(M_CANTWRIT, "");
  379.         }
  380.     }
  381.     else
  382.     {
  383.         if (file_write((char *)p, 1, n, outfile) != n)
  384.             error(M_CANTWRIT, "");
  385.     }
  386. }
  387.  
  388. void
  389. init_getbits()
  390. {
  391.     bitbuf = 0;
  392.     subbitbuf = 0;
  393.     bitcount = 0;
  394.     fillbuf(2 * CHAR_BIT);
  395. }
  396.  
  397. void
  398. fillbuf(n)                /* Shift bitbuf n bits left, read n bits */
  399. int n;
  400. {
  401.     bitbuf = (bitbuf << n) & 0xFFFF;  /* lose the first n bits */
  402.     while (n > bitcount)
  403.     {
  404.         bitbuf |= subbitbuf << (n -= bitcount);
  405.         if (compsize != 0)
  406.         {
  407.             compsize--;
  408.             subbitbuf = (uchar) getc(arcfile);
  409.         }
  410.         else
  411.             subbitbuf = 0;
  412.         bitcount = CHAR_BIT;
  413.     }
  414.     bitbuf |= subbitbuf >> (bitcount -= n);
  415. }
  416.  
  417. ushort
  418. getbits(n)
  419. int n;
  420. {
  421.     ushort x;
  422.  
  423.     x = bitbuf >> (2 * CHAR_BIT - n);
  424.     fillbuf(n);
  425.     return x;
  426. }
  427.  
  428. static void
  429. decode_path(name)
  430. char *name;
  431. {
  432.     for ( ; *name; name++)
  433.     {
  434.         if (*name == ARJ_PATH_CHAR)
  435.             *name = PATH_CHAR;
  436.     }
  437. }
  438.  
  439. static void
  440. get_date_str(str, tstamp)
  441. char  *str;
  442. ulong tstamp;
  443. {
  444.     sprintf(str, "%04u-%02u-%02u %02u:%02u:%02u",
  445.            ts_year(tstamp), ts_month(tstamp), ts_day(tstamp),
  446.            ts_hour(tstamp), ts_min(tstamp), ts_sec(tstamp));
  447. }
  448.  
  449. static int
  450. parse_path(pathname, path, entry)
  451. char *pathname;
  452. char *path;
  453. char *entry;
  454. {
  455.     char *cptr, *ptr, *fptr;
  456.     short pos;
  457.  
  458.     fptr = NULL;
  459.     for (cptr = PATH_SEPARATORS; *cptr; cptr++)
  460.     {
  461.         if ((ptr = strrchr(pathname, *cptr)) != NULL &&
  462.                 (fptr == NULL || ptr > fptr))
  463.             fptr = ptr;
  464.     }
  465.     if (fptr == NULL)
  466.         pos = 0;
  467.     else
  468.         pos = fptr + 1 - pathname;
  469.     if (path != NULL)
  470.     {
  471.        strncpy(path, pathname, pos);
  472.        path[pos] = NULL_CHAR;
  473.     }
  474.     if (entry != NULL)
  475.        strcpy(entry, &pathname[pos]);
  476.     return pos;
  477. }
  478.  
  479. static void
  480. strncopy(to, from, len)
  481. char *to;
  482. char *from;
  483. int  len;
  484. {
  485.     int i;
  486.  
  487.     for (i = 1; i < len && *from; i++)
  488.         *to++ = *from++;
  489.     *to = NULL_CHAR;
  490. }
  491.  
  492. void
  493. strlower(s)
  494. char *s;
  495. {
  496.     while (*s)
  497.     {
  498.         *s = (char) tolower(*s);
  499.         s++;
  500.     }
  501. }
  502.  
  503. void
  504. strupper(s)
  505. char *s;
  506. {
  507.     while (*s)
  508.     {
  509.         *s = (char) toupper(*s);
  510.         s++;
  511.     }
  512. }
  513.  
  514. voidp *
  515. malloc_msg(size)
  516. int size;
  517. {
  518.     char *p;
  519.  
  520.     if ((p = (char *)xmalloc(size)) == NULL)
  521.         error(M_NOMEMORY, "");
  522.     return (voidp *)p;
  523. }
  524.  
  525. static uint
  526. get_word()
  527. {
  528.     uint b0, b1;
  529.  
  530.     b0 = get_byte();
  531.     b1 = get_byte();
  532.     return (b1 << 8) + b0;
  533. }
  534.  
  535. static ulong
  536. get_longword()
  537. {
  538.     ulong b0, b1, b2, b3;
  539.  
  540.     b0 = get_byte();
  541.     b1 = get_byte();
  542.     b2 = get_byte();
  543.     b3 = get_byte();
  544.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  545. }
  546.  
  547. static long
  548. find_header(fd)
  549. FILE *fd;
  550. {
  551.     long arcpos, lastpos;
  552.     int c;
  553.  
  554.     arcpos = file_tell(fd);
  555.     file_seek(fd, 0L, SEEK_END);
  556.     lastpos = file_tell(fd) - 2;
  557.     if (lastpos > MAXSFX)
  558.         lastpos = MAXSFX;
  559.     for ( ; arcpos < lastpos; arcpos++)
  560.     {
  561.         file_seek(fd, arcpos, SEEK_SET);
  562.         c = fget_byte(fd);
  563.         while (arcpos < lastpos)
  564.         {
  565.             if (c != HEADER_ID_LO)  /* low order first */
  566.                 c = fget_byte(fd);
  567.             else if ((c = fget_byte(fd)) == HEADER_ID_HI)
  568.                 break;
  569.             arcpos++;
  570.         }
  571.         if (arcpos >= lastpos)
  572.             break;
  573.         if ((headersize = fget_word(fd)) <= HEADERSIZE_MAX)
  574.         {
  575.             crc = CRC_MASK;
  576.             fread_crc(header, (int) headersize, fd);
  577.             if ((crc ^ CRC_MASK) == fget_crc(fd))
  578.             {
  579.                 file_seek(fd, arcpos, SEEK_SET);
  580.                 return arcpos;
  581.             }
  582.         }
  583.     }
  584.     return -1;          /* could not find a valid header */
  585. }
  586.  
  587. static int
  588. read_header(first, fd, name)
  589. int  first;
  590. FILE *fd;
  591. char *name;
  592. {
  593.     ushort extheadersize, header_id;
  594.  
  595.     header_id = fget_word(fd);
  596.     if (header_id != HEADER_ID)
  597.     {
  598.         if (first)
  599.             error(M_NOTARJ, name);
  600.         else
  601.             error(M_BADHEADR, "");
  602.     }
  603.  
  604.     headersize = fget_word(fd);
  605.     if (headersize == 0)
  606.         return 0;               /* end of archive */
  607.     if (headersize > HEADERSIZE_MAX)
  608.         error(M_BADHEADR, "");
  609.  
  610.     crc = CRC_MASK;
  611.     fread_crc(header, (int) headersize, fd);
  612.     header_crc = fget_crc(fd);
  613.     if ((crc ^ CRC_MASK) != header_crc)
  614.         error(M_HEADRCRC, "");
  615.  
  616.     setup_get(header);
  617.     first_hdr_size = get_byte();
  618.     arj_nbr = get_byte();
  619.     arj_x_nbr = get_byte();
  620.     host_os = get_byte();
  621.     arj_flags = get_byte();
  622.     method = get_byte();
  623.     file_type = get_byte();
  624.     (void)get_byte();
  625.     time_stamp = get_longword();
  626.     compsize = get_longword();
  627.     origsize = get_longword();
  628.     file_crc = get_crc();
  629.     entry_pos = get_word();
  630.     file_mode = get_word();
  631.     host_data = get_word();
  632.  
  633.     hdr_filename = (char *)&header[first_hdr_size];
  634.     strncopy(filename, hdr_filename, sizeof(filename));
  635.     if (host_os != OS)
  636.         strparity((uchar *)filename);
  637.     if ((arj_flags & PATHSYM_FLAG) != 0)
  638.         decode_path(filename);
  639.  
  640.     hdr_comment = (char *)&header[first_hdr_size + strlen(hdr_filename) + 1];
  641.     strncopy(comment, hdr_comment, sizeof(comment));
  642.     if (host_os != OS)
  643.         strparity((uchar *)comment);
  644.  
  645.     /* if extheadersize == 0 then no CRC */
  646.     /* otherwise read extheader data and read 4 bytes for CRC */
  647.  
  648.     while ((extheadersize = fget_word(fd)) != 0)
  649.         file_seek(fd, (long) (extheadersize + 4), SEEK_CUR);
  650.  
  651.     return 1;                   /* success */
  652. }
  653.  
  654. static void
  655. skip()
  656. {
  657.     file_seek(arcfile, compsize, SEEK_CUR);
  658. }
  659.  
  660. static void
  661. unstore()
  662. {
  663.     int n;
  664.     long pos;
  665.     char *buffer;
  666.  
  667.     buffer = (char *)malloc_msg(BUFFERSIZE);
  668.     pos = file_tell(arcfile);
  669.     disp_clock();
  670.     n = (int)(BUFFERSIZE - (pos % BUFFERSIZE));
  671.     n = compsize > (long)n ? n : (int)compsize;
  672.     while (compsize > 0)
  673.     {
  674.         if (file_read(buffer, 1, n, arcfile) != n)
  675.             error(M_CANTREAD, "");
  676.         disp_clock();
  677.         compsize -= n;
  678.         fwrite_txt_crc((uchar *)buffer, n);
  679.         n = compsize > BUFFERSIZE ? BUFFERSIZE : (int)compsize;
  680.     }
  681.     free(buffer);
  682. }
  683.  
  684. static int
  685. check_flags()
  686. {
  687.     if (arj_x_nbr > ARJ_X_VERSION)
  688.     {
  689.         printf(M_UNKNVERS, arj_x_nbr);
  690.         printf(M_SKIPPED, filename);
  691.         skip();
  692.         return -1;
  693.     }
  694.     if ((arj_flags & GARBLE_FLAG) != 0)
  695.     {
  696.         printf(M_ENCRYPT);
  697.         printf(M_SKIPPED, filename);
  698.         skip();
  699.         return -1;
  700.     }
  701.     if (method < 0 || method > MAXMETHOD || (method == 4 && arj_nbr == 1))
  702.     {
  703.         printf(M_UNKNMETH, method);
  704.         printf(M_SKIPPED, filename);
  705.         skip();
  706.         return -1;
  707.     }
  708.     if (file_type != BINARY_TYPE && file_type != TEXT_TYPE)
  709.     {
  710.         printf(M_UNKNTYPE, file_type);
  711.         printf(M_SKIPPED, filename);
  712.         skip();
  713.         return -1;
  714.     }
  715.     return 0;
  716. }
  717.  
  718. static int
  719. extract()
  720. {
  721.     char name[FNAME_MAX];
  722.  
  723.     if (check_flags())
  724.     {
  725.         error_count++;
  726.         return 0;
  727.     }
  728.  
  729.     no_output = 0;
  730.     if (command == 'E')
  731.         strcpy(name, &filename[entry_pos]);
  732.     else
  733.     {
  734.         strcpy(name, DEFAULT_DIR);
  735.         strcat(name, filename);
  736.     }
  737.  
  738.     if (host_os != OS)
  739.         default_case_path(name);
  740.  
  741.     if (file_exists(name))
  742.     {
  743.         printf(M_FEXISTS, name);
  744.         printf(M_SKIPPED, name);
  745.         skip();
  746.         error_count++;
  747.         return 0;
  748.     }
  749.     outfile = file_open(name, writemode[file_type & 1]);
  750.     if (outfile == NULL)
  751.     {
  752.         printf(M_CANTOPEN, name);
  753.         putchar('\n');
  754.         skip();
  755.         error_count++;
  756.         return 0;
  757.     }
  758.     printf(M_EXTRACT, name);
  759.     if (host_os != OS && file_type == BINARY_TYPE)
  760.         printf(M_DIFFHOST);
  761.     printf("  ");
  762.  
  763.     crc = CRC_MASK;
  764.  
  765.     if (method == 0)
  766.         unstore();
  767.     else if (method == 1 || method == 2 || method == 3)
  768.         decode();
  769.     else if (method == 4)
  770.         decode_f();
  771.     fclose(outfile);
  772.  
  773.     set_ftime_mode(name, time_stamp, file_mode, (uint) host_os);
  774.  
  775.     if ((crc ^ CRC_MASK) == file_crc)
  776.         printf(M_CRCOK);
  777.     else
  778.     {
  779.         printf(M_CRCERROR);
  780.         error_count++;
  781.     }
  782.     return 1;
  783. }
  784.  
  785. static int
  786. test()
  787. {
  788.     if (check_flags())
  789.         return 0;
  790.  
  791.     no_output = 1;
  792.     printf(M_TESTING, filename);
  793.     printf("  ");
  794.  
  795.     crc = CRC_MASK;
  796.  
  797.     if (method == 0)
  798.         unstore();
  799.     else if (method == 1 || method == 2 || method == 3)
  800.         decode();
  801.     else if (method == 4)
  802.         decode_f();
  803.  
  804.     if ((crc ^ CRC_MASK) == file_crc)
  805.         printf(M_CRCOK);
  806.     else
  807.     {
  808.         printf(M_CRCERROR);
  809.         error_count++;
  810.     }
  811.     return 1;
  812. }
  813.  
  814. static uint
  815. ratio(a, b)
  816. long a, b;
  817. {
  818.    int i;
  819.  
  820.    for (i = 0; i < 3; i++)
  821.        if (a <= LONG_MAX / 10)
  822.            a *= 10;
  823.        else
  824.            b /= 10;
  825.    if ((long) (a + (b >> 1)) < a)
  826.    {
  827.        a >>= 1;
  828.        b >>= 1;
  829.    }
  830.    if (b == 0)
  831.        return 0;
  832.    return (uint) ((a + (b >> 1)) / b);
  833. }
  834.  
  835. static void
  836. list_start()
  837. {
  838.     printf("Filename       Original Compressed Ratio DateTime modified CRC-32   AttrBTPMGVX\n");
  839.     printf("------------ ---------- ---------- ----- ----------------- -------- -----------\n");
  840. }
  841.  
  842. static void
  843. list_arc(count)
  844. int count;
  845. {
  846.     uint r;
  847.     int garble_mode, path_mode, volume_mode, extfil_mode, ftype, bckf_mode;
  848.     char date_str[20], fmode_str[10];
  849.     static char mode[5] = { 'B', 'T', '?', 'D', 'V' };
  850.     static char pthf[2] = { ' ', '+' };
  851.     static char pwdf[2] = { ' ', 'G' };  /* plain, encrypted */
  852.     static char volf[2] = { ' ', 'V' };
  853.     static char extf[2] = { ' ', 'X' };
  854.     static char bckf[2] = { ' ', '*' };
  855.  
  856.     if (count == 0)
  857.         list_start();
  858.  
  859.     garble_mode = ((arj_flags & GARBLE_FLAG) != 0);
  860.     volume_mode = ((arj_flags & VOLUME_FLAG) != 0);
  861.     extfil_mode = ((arj_flags & EXTFILE_FLAG) != 0);
  862.     bckf_mode   = ((arj_flags & BACKUP_FLAG) != 0);
  863.     path_mode   = (entry_pos > 0);
  864.     r = ratio(compsize, origsize);
  865.     torigsize += origsize;
  866.     tcompsize += compsize;
  867.     ftype = file_type;
  868.     if (ftype != BINARY_TYPE && ftype != TEXT_TYPE && ftype != DIR_TYPE &&
  869.             ftype != LABEL_TYPE)
  870.         ftype = 3;
  871.     get_date_str(date_str, time_stamp);
  872.     strcpy(fmode_str, "    ");
  873.     if (host_os == OS)
  874.         get_mode_str(fmode_str, (uint) file_mode);
  875.     if (strlen(&filename[entry_pos]) > 12)
  876.         printf("%-12s\n             ", &filename[entry_pos]);
  877.     else
  878.         printf("%-12s ", &filename[entry_pos]);
  879.     printf("%10ld %10ld %u.%03u %s %08lX %4s%c%c%c%u%c%c%c\n",
  880.         origsize, compsize, r / 1000, r % 1000, &date_str[2], file_crc,
  881.         fmode_str, bckf[bckf_mode], mode[ftype], pthf[path_mode], method,
  882.         pwdf[garble_mode], volf[volume_mode], extf[extfil_mode]);
  883. }
  884.  
  885. static void
  886. execute_cmd()
  887. {
  888.     int file_count;
  889.     char date_str[22];
  890.     uint r;
  891.  
  892.     first_hdr_pos = 0;
  893.     time_stamp = 0;
  894.     first_hdr_size = FIRST_HDR_SIZE;
  895.  
  896.     arcfile = fopen_msg(arc_name, "rb");
  897.  
  898.     printf(M_PROCARC, arc_name);
  899.  
  900.     first_hdr_pos = find_header(arcfile);
  901.     if (first_hdr_pos < 0)
  902.         error(M_NOTARJ, arc_name);
  903.     file_seek(arcfile, first_hdr_pos, SEEK_SET);
  904.     if (!read_header(1, arcfile, arc_name))
  905.         error(M_BADCOMNT, "");
  906.     get_date_str(date_str, time_stamp);
  907.     printf(M_ARCDATE, date_str);
  908.     if (arj_nbr >= ARJ_M_VERSION)
  909.     {
  910.         get_date_str(date_str, (ulong) compsize);
  911.         printf(M_ARCDATEM, date_str);
  912.     }
  913.     printf("\n");
  914.  
  915.     file_count = 0;
  916.     while (read_header(0, arcfile, arc_name))
  917.     {
  918.         switch (command)
  919.         {
  920.         case 'E':
  921.         case 'X':
  922.             if (extract())
  923.                 file_count++;
  924.             break;
  925.         case 'L':
  926.             list_arc(file_count++);
  927.             skip();
  928.             break;
  929.         case 'T':
  930.             if (test())
  931.                 file_count++;
  932.             break;
  933.         }
  934.     }
  935.  
  936.     if (command == 'L')
  937.     {
  938.         printf("------------ ---------- ---------- ----- -----------------\n");
  939.         r = ratio(tcompsize, torigsize);
  940.         printf(" %5d files %10ld %10ld %u.%03u %s\n",
  941.             file_count, torigsize, tcompsize, r / 1000, r % 1000, &date_str[2]);
  942.     }
  943.     else
  944.         printf(M_NBRFILES, file_count);
  945.  
  946.     fclose(arcfile);
  947. }
  948.  
  949. static void
  950. help()
  951. {
  952.     int i;
  953.  
  954.     for (i = 0; M_USAGE[i] != NULL; i++)
  955.         printf(M_USAGE[i]);
  956. }
  957.  
  958. int
  959. main(argc, argv)
  960. int  argc;
  961. char *argv[];
  962. {
  963.     int i, j, lastc;
  964.     char *arc_p;
  965.  
  966. #ifdef THINK_C
  967.     argc = ccommand(&argv);
  968. #endif
  969.  
  970.     printf(M_VERSION);
  971.  
  972.     if (argc == 1)
  973.     {
  974.         help();
  975.         return EXIT_SUCCESS;
  976.     }
  977.     else if (argc == 2)
  978.     {
  979.         command = 'L';
  980.         arc_p = argv[1];
  981.     }
  982.     else if (argc == 3)
  983.     {
  984.         if (strlen(argv[1]) > 1)
  985.             error(M_BADCOMND, argv[1]);
  986.         command = toupper(*argv[1]);
  987.         if (strchr("ELTX", command) == NULL)
  988.             error(M_BADCOMND, argv[1]);
  989.         arc_p = argv[2];
  990.     }
  991.     else
  992.     {
  993.         help();
  994.         return EXIT_FAILURE;
  995.     }
  996.  
  997.     strncopy(arc_name, arc_p, FNAME_MAX);
  998.     case_path(arc_name);
  999.     i = strlen(arc_name);
  1000.     j = parse_path(arc_name, (char *)NULL, (char *)NULL);
  1001.     lastc = arc_name[i - 1];
  1002.     if (lastc == ARJ_DOT)
  1003.         arc_name[i - 1] = NULL_CHAR;
  1004.     else if (strchr(&arc_name[j], ARJ_DOT) == NULL)
  1005.         strcat(arc_name, M_SUFFIX);
  1006.  
  1007.     make_crctable();
  1008.  
  1009.     error_count = 0;
  1010.     clock_inx = 0;
  1011.     arcfile = NULL;
  1012.     outfile = NULL;
  1013.  
  1014.     execute_cmd();
  1015.  
  1016.     if (error_count > 0)
  1017.         error(M_ERRORCNT, "");
  1018.  
  1019.     return EXIT_SUCCESS;
  1020. }
  1021.  
  1022. /* end UNARJ.C */
  1023.